/** * Copyright (C) 2012 alanhay <alanhay99@hotmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package uk.co.certait.htmlexporter.css; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import com.osbcp.cssparser.CSSParser; import com.osbcp.cssparser.Rule; import com.osbcp.cssparser.Selector; public class StyleParser { private StyleGenerator generator; public StyleParser() { generator = new StyleGenerator(); } public Map<String, Style> parseStyles(Elements elements) { Map<String, Style> styles = new HashMap<>(); for (Element element : elements) { try { List<Rule> rules = CSSParser.parse(element.data()); mapStyles(rules, styles); } catch (Exception e) { throw new RuntimeException(e); } } return styles; } protected void mapStyles(List<Rule> rules, Map<String, Style> styles) { for (Rule rule : rules) { for (Selector selector : rule.getSelectors()) { Style style = generator.createStyle(rule, selector); if (styles.containsKey(selector.toString())) { Style merged = StyleMerger.mergeStyles(styles.get(selector.toString()), style); styles.put(selector.toString(), merged); } else { styles.put(selector.toString(), style); } } } } }